package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    //根据id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new BrandDTO(this.brandService.getById(id)));
    }

    //根据id品牌集合查询品牌集合
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(BrandDTO.convertEntityList(this.brandService.listByIds(ids)));
    }

    //分页查询
/*
* RequestParam来映射请求参数
required表示是否必须,默认为true
defaultValue请求参数的默认值
* */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key) {

        return ResponseEntity.ok(this.brandService.pageQuery(page, rows, key));
    }

    //品牌新增
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO) {

        this.brandService.saveBrand(brandDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据分类ID查询对应的品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(this.brandService.queryBrandByCategoryId(cid));
    }

    //根据ID删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id) {
        this.brandService.deleteBrandById(id);
        return ResponseEntity.ok().build();
    }

    //品牌修改
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        this.brandService.updateBrand(brandDTO);
        //只返回状态码，
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
